﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第八次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool C=false;
        double front = 0;
        double result = 0;
        string op="";
        public void NumberClicked(object sender, EventArgs e)
        {//数字按钮点击触发事件（多按钮响应一个事件）
            try
            {
                if (C == true)
                {
                    textBox1.Text = "";
                    C = false;
                }
                textBox1.Text += ((Button)sender).Text;
            }
            catch { }
        }
        private void Plus_Click(object sender, EventArgs e)
        {//求和按钮点击事件
            try
            {
                C = true;
                front = double.Parse(textBox1.Text);
                op = "+";
                textBox1.Text = "+";
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }

        private void Minus_Click(object sender, EventArgs e)
        {//求差按钮点击事件
            try
            {
                C = true;
                front = double.Parse(textBox1.Text);
                op = "-";
                textBox1.Text = "-";
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }

        private void Multipy_Click(object sender, EventArgs e)
        {//求积按钮触发事件
            try
            {
                C = true;
                front = double.Parse(textBox1.Text);
                op = "*";
                textBox1.Text = "*";
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }

        private void Divide_Click(object sender, EventArgs e)
        {//求商按钮触发事件
            try
            {
                C = true;
                front = double.Parse(textBox1.Text);
                op = "/";
                textBox1.Text = "/";
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }

        private void changeC_Click(object sender, EventArgs e)
        {//清零按钮触发事件
            try
            {
                textBox1.Text = "";
                front = 0;
                result = 0;
                op = "";
                C = false;
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }

        private void Square_Click(object sender, EventArgs e)
        {//求平方按钮触发事件
            try
            {
                C = true;
                front = double.Parse(textBox1.Text);
                op = "square";
                textBox1.Text = front+"²";
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }
        private void Log_Click(object sender, EventArgs e)
        {//取对数按钮触发事件（以e为底）
            try
            {
                C = true;
                front = double.Parse(textBox1.Text);
                op = "log";
                textBox1.Text = "ln"+"("+front+")";
            }
            catch (Exception)
            {
                MessageBox.Show("输入顺序错误！");
                C = true;
            }
        }

        private void Equal_Click(object sender, EventArgs e)
        {//等号按钮触发事件
            try
            {
                switch (op)
                {
                    case "+": 
                        result = front + double.Parse(textBox1.Text);
                        textBox1.Text = front + op + double.Parse(textBox1.Text)+"="+result;
                        break;
                    case "-":
                        result = front - double.Parse(textBox1.Text);
                        textBox1.Text = front + op + double.Parse(textBox1.Text)+"="+result; 
                        break;
                    case "*": 
                        result = front * double.Parse(textBox1.Text);
                        textBox1.Text = front + op + double.Parse(textBox1.Text)+"="+result; 
                        break;
                    case "/": 
                        result = front / double.Parse(textBox1.Text);
                        textBox1.Text = front + op + double.Parse(textBox1.Text)+"="+result; 
                        break;
                    case "square":
                        result = front*front;
                        textBox1.Text = front + "²" + "=" + result;
                        break;
                    case "log":
                        result = Math.Log(front);
                        textBox1.Text = "ln(" + front + ")" + "=" + result;
                        break;
                }
                C = true;
            }
            catch (Exception)
            {
                MessageBox.Show("输入错误,请重试！");
                C = true;
            }
        }

        private void Reverse_Click(object sender, EventArgs e)
        {//求反按钮触发事件
            textBox1.Text = -double.Parse(textBox1.Text) + "";
        }
    }
}
